package cn.trigram.file;

import cn.trigram.asserts.AssertUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class StreamUtil {

  public static final int BUFFER_SIZE = 4096;

  public static int copy(InputStream in, OutputStream out) throws IOException {

    AssertUtil.assertNotNull(in, "No InputStream specified");
    AssertUtil.assertNotNull(out, "No OutputStream specified");

    int    byteCount = 0;
    byte[] buffer    = new byte[BUFFER_SIZE];
    int    bytesRead;
    while ((bytesRead = in.read(buffer)) != -1) {
      out.write(buffer, 0, bytesRead);
      byteCount += bytesRead;
    }
    out.flush();
    return byteCount;
  }

}
